//
//  testInitializeGame.c
//  dominion
//
//  Created by Doug Mason on 3/8/14.
//  Copyright (c) 2014 Doug Mason. All rights reserved.
//

#include <stdio.h>
#include "testInitializeGame.h"
//TODO : write our test
/* Ensure that our players will all start with the correct number of cards in their decks, hands ,and discard piles at the initialization of the game.
 That the correct number of coins and buys are in play and that the first player is ready to play at the end of initialization.

 */
int testInitializeGame(int numPlayers, int kingdomCards[10], int randomSeed,
                       struct gameState *state){
    int numErrors = 0;
    int i,j;
    int initResult = initializeGame(numPlayers,kingdomCards,randomSeed,state);
    if(initResult == -1){
      return -1;
    }
    if(numPlayers <=1){
        if (initResult!=-1) {
            printf("Failed to fail on not having enough players! \n");
            numErrors+=1;
        }
    }
    if (numPlayers == 2) {
        //tests
        if(state->supplyCount[curse]!=10){
            printf("Failed to add curses for %d players\n",numPlayers);
            numErrors+=1;
        }
        if (state->supplyCount[estate]!=8) {
            printf("Failed to initialize the correct number of estates!\n");
            numErrors+=1;
        }
        if (state->supplyCount[duchy]!=8) {
            printf("Failed to initialize the correct number of duchies!\n");
            numErrors+=1;
        }
        if (state->supplyCount[province]!=8) {
            printf("Failed to initialize the correct number of provinences!\n");
            numErrors+=1;
        }

        if(state->supplyCount[copper] != 60 - (7 * numPlayers)){
            printf("Failed to initialize the correct number of copper!\n");
            numErrors+=1;
        }
        if(state->supplyCount[silver] != 40){
            printf("Failed to initialize the correct number of silver!\n");
            numErrors+=1;
        }
        if(state->supplyCount[gold] != 30){
            printf("Failed to initialize the correct number of gold!\n");
            numErrors+=1;
        }


    }
    else if(numPlayers==3){
        if(state->supplyCount[curse]!=20){
            printf("Failed to add curses for %d players\n",numPlayers);
        }
        if (state->supplyCount[estate]!=12) {
            printf("Failed to initialize the correct number of estates!\n");
            numErrors+=1;
        }
        if (state->supplyCount[duchy]!=12) {
            printf("Failed to initialize the correct number of duchies!\n");
            numErrors+=1;
        }
        if (state->supplyCount[province]!=12) {
            printf("Failed to initialize the correct number of provinces!\n");
            numErrors+=1;
        }

        if(state->supplyCount[copper] != 60 - (7 * numPlayers)){
            printf("Failed to initialize the correct number of copper!\n");
            numErrors+=1;
        }
        if(state->supplyCount[silver] != 40){
            printf("Failed to initialize the correct number of silver!\n");
            numErrors+=1;
        }
        if(state->supplyCount[gold] != 30){
            printf("Failed to initialize the correct number of gold!\n");
            numErrors+=1;
        }
    }
    //since max players is 4..
    else{
        if(state->supplyCount[curse]!=30){
            printf("Failed to add curses for %d players\n",numPlayers);
        }
        if (state->supplyCount[estate]!=12) {
            printf("Failed to initialize the correct number of estates!\n");
            numErrors+=1;
        }
        if (state->supplyCount[duchy]!=12) {
            printf("Failed to initialize the correct number of duchies!\n");
            numErrors+=1;
        }
        if (state->supplyCount[province]!=12) {
            printf("Failed to initialize the correct number of provinces!\n");
            numErrors+=1;
        }

        if(state->supplyCount[copper] != 60 - (7 * numPlayers)){
            printf("Failed to initialize the correct number of copper!\n");
            numErrors+=1;
        }
        if(state->supplyCount[silver] != 40){
            printf("Failed to initialize the correct number of silver!\n");
            numErrors+=1;
        }
        if(state->supplyCount[gold] != 30){
            printf("Failed to initialize the correct number of gold!\n");
            numErrors+=1;
        }

    }

    for(j = adventurer; j > treasure_map;j++){
      if(state->supplyCount[j]==8){
        if(j == gardens || j == great_hall){
          if(state->numPlayers==2){
            printf("Successfully allocated the right number of %i for game.\n",j);

          }
          else{
            printf("Unsuccessfully allocated 8 cards when it should have been 12.\n");
            numErrors+=1;
          }
        }
      }
      else if(state->supplyCount[j]==12){
        if(j == gardens || j == great_hall){
          if(state->numPlayers>=3){
            printf("Successfully allocated the right number of %i for game.\n",j);

          }
          else{
            printf("Unsuccessfully allocated 12 cards when it should have been 8.\n");
            numErrors+=1;
          }
        }
      }
      else if(state->supplyCount[j]==10){
        for(i = 0; i < 10; i++){
          if(kingdomCards[i]==j){
            printf("Successfully allocated correct number of card %i in play.",j);
          }
          else{
            printf("This card shouldn't be in play at the moment!");
            numErrors+=1;
          }
        }
      }
    }
    if(state->outpostPlayed!=0){
        printf("Failed to initialize correct number of outposts in play.\n");
        numErrors+=1;
    }
    if(state->phase!=0){
        printf("Failed to initialize correct first phase.\n");
        numErrors+=1;
    }
    if (state->numActions!=1) {
        printf("Failed to initialize correct number actions in play.\n");
        numErrors+=1;
    }
    if(state->numBuys!=1){
        printf("Failed to initialize correct number of buys.\n");
        numErrors+=1;
    }
    if(state->whoseTurn!=0){
        printf("Failed to setup first player");
        numErrors+=1;
    }

    if(state->playedCardCount!=0){
        printf("Failed to reset the played card count!\n");
        numErrors+=1;
    }
    for(i = 1; i < numPlayers; i++){
        if (state->deckCount[i]!=10) {
            printf("Did not deal 10 cards to unused player %d.",i);
            numErrors+=1;
        }
    }
    if (state->handCount[0]!=5) {
        printf("Failed to draw hand correctly for first player %d cards in hand.\n",state->handCount[0]);
        numErrors+=1;
    }
    if(state->deckCount[0]!=5){
        printf("Invalid initial deck for first player %d cards in deck.\n",state->deckCount[0]);
        numErrors+=1;
    }
    if(numErrors == 0){
        printf("All tests for testInitializeGame passed!\n");
        return 0;
    }
    else{
        printf("Number of tests that have failed: %d for initialization. \n",numErrors);
        return -1;
    }
}
